package com.momo.rocketmqapi.quickstart;

import com.momo.rocketmqapi.constants.Const;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import javax.validation.constraints.Size;

/**
 * @program: momo-cloud
 * @description: TODO
 * @author: Jie Li
 * @create: 2019-07-08 21:58
 **/
@Slf4j
public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, MQBrokerException, InterruptedException{
        DefaultMQProducer producer=new DefaultMQProducer("test_quick_producer_name");
        producer.setNamesrvAddr(Const.NAMESRV_ADDR_MASTER_SLAVE);
        producer.start();
        for(int i = 0 ; i <5; i ++) {
            //	1.	创建消息
            Message message = new Message("test_quick_topic",    //	主题
                    "TagA", //	标签
                    "key" + i,    // 	用户自定义的key ,唯一的标识
                    ("Hello RocketMQ" + i).getBytes());    //	消息内容实体（byte[]）
            //发送消息

            SendResult sr= producer.send(message);
            SendStatus status = sr.getSendStatus();
            System.out.println(status);
			log.info("状态{}",status);
            System.err.println("消息发出: " + sr);
        }
        producer.shutdown();
    }
}
